package cn.ffcs.uec.outStaffAdjustPostApply.component;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.organization.manager.UecOrgDeptManager;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.organization.model.UecOrgDept;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uec.outStaff.constant.UecOutStaffConstants;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.outStaff.vo.UecOutStaffInfoVo;
import cn.ffcs.uec.outStaffAdjustPostApply.component.bean.OutStaffAdjustPostApplyListboxExtBean;
import cn.ffcs.uec.staff.manager.UecPsndocWadocManager;
import cn.ffcs.uec.staff.model.UecPsndocWadoc;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import lombok.Getter;
import lombok.Setter;

@Controller
@Scope("prototype")
public class OutStaffAdjustPostApplyListboxExt extends Div implements IdSpace {

	private static final long serialVersionUID = -4175560615607776233L;

	private static final String OUT_STAFF_ADJUST_POST_APPLY_LISTBOX_EXT_ZUL = "/pages_nmg/outStaffAdjustPostApply/comp/outStaffAdjustPostApply_listbox_ext.zul";
	public static final String OUT_STAFF_ADJUST_POST_EDIT_APPLY_ZUL = "/pages_nmg/outStaffAdjustPostApply/comp/outStaffAdjustPostApply_edit.zul";
	@Getter
	@Setter
	private OutStaffAdjustPostApplyListboxExtBean bean = new OutStaffAdjustPostApplyListboxExtBean();

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	/**
	 * 外包人员基本信息对象
	 */
	private UecOutStaffInfo uecOutStaffInfo;
	
	/**
	 * 外包人员虚拟对象
	 */
	private UecOutStaffInfoVo uecOutStaffInfoVo;

	/**
	 * 薪资变动情况
	 */
	private UecPsndocWadoc uecPsndocWadoc;

	private UecPsndocWadocManager uecPsndocWadocManager = (UecPsndocWadocManager) ApplicationContextUtil
			.getBean("uecPsndocWadocManager");
	private UecOrgDeptManager uecOrgDeptManager = (UecOrgDeptManager) ApplicationContextUtil
			.getBean("uecOrgDeptManager");
	private UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
			.getBean("uecOrgOrgsManager");
	public OutStaffAdjustPostApplyListboxExt() {
		Executions.createComponents(OUT_STAFF_ADJUST_POST_APPLY_LISTBOX_EXT_ZUL, this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');
	}

	/**
	 * 界面初始化
	 */
	public void onCreate() {
		bandbox();
	}

	/**
	 * 选择查询记录
	 * onDoubleClick=onUecPsndocWdocSelectRequest 双击
	 * @throws Exception
	 */
	public void onUecPsndocWdocSelectRequest() throws Exception {
		if (this.bean.getOutStaffAdjustPostApplyListbox().getSelectedCount() > 0) {
			uecPsndocWadoc = (UecPsndocWadoc) bean.getOutStaffAdjustPostApplyListbox().getSelectedItem().getValue();
			if(null != uecPsndocWadoc) {
				if(!"Y".equals(uecPsndocWadoc.getLastflag())) {
					ZkUtil.showError("请选择最新的薪资记录进行调薪操作", "温馨提示");
					return;
				}else {
					this.onAddAdjustPost();
				}
			}
		}
	}

	/**
	 * 查询
	 * @throws Exception 
	 * @throws SuspendNotAllowedException 
	 */
	public void onQuery() throws SuspendNotAllowedException, Exception {

		clearVariable();
		PubUtil.clearListbox(bean.getOutStaffAdjustPostApplyListbox());
		uecOutStaffInfo = UecOutStaffInfo.newInstance();
		uecOutStaffInfoVo = new UecOutStaffInfoVo();
		/**
		 * 组织树Ext
		 * UecOrganizationTreeBandboxExt
		 * 组织树上有组织和部门
		 * 通过组织树获取组织或部门
		 */
		if(!StrUtil.isNullOrEmpty(bean.getOutStaffPkHrorg().getValue())) {
			//组织类型：公司 or 部门
			String orgType = (String) bean.getOutStaffPkHrorg().getOrganization().getOrgType();
			//组织主键
			String pkorg = (String) bean.getOutStaffPkHrorg().getOrganization().getOrg();
			if ("ORG".equals(orgType)) {
				String orgName = (String) bean.getOutStaffPkHrorg().getOrganization().getOrgName();
				// 公司
				//uecOutStaffInfoVo.setPkOrg(pkorg);
				 if("中国电信股份有限公司内蒙古分公司".equals(orgName)) {
			       		uecOutStaffInfoVo
			       		.setOrgName(null);
			       	}else if(orgName.endsWith("*")){
			       		uecOutStaffInfoVo
			       		.setOrgName(orgName);
			       	}else {
			       		uecOutStaffInfoVo.setPkOrg(pkorg);
			       	}
			} else {
				// 部门
				UecOrgDept uecOrgDept = new UecOrgDept();
				UecOrgOrgs uecOrgOrgs = new UecOrgOrgs();
				uecOrgDept.setPkDept(pkorg);
				uecOrgDept = uecOrgDeptManager.findUecOrgDeptByOrgDept(uecOrgDept);
				// 部门所属公司
				if(uecOrgDept !=null) {
					uecOrgOrgs= new UecOrgOrgs();
					uecOrgOrgs.setPkOrg(uecOrgDept.getPkOrg());
					uecOrgOrgs = uecOrgOrgsManager.findUecOrgOrgsByUecOrgOrgs(uecOrgOrgs);
					String orgName=uecOrgOrgs.getName();
					uecOutStaffInfoVo.setPkDept(uecOrgDept.getPkDept());
					uecOutStaffInfoVo.setPkOrg(uecOrgDept.getPkOrg());
					uecOutStaffInfoVo.setOrgName(orgName);

				//uecOutStaffInfoVo.setPkDept(pkorg);
			}
			}
		}
		uecOutStaffInfoVo.setStaffCode(bean.getOutStaffCode().getValue());
		uecOutStaffInfoVo.setStaffName(bean.getOutStaffName().getValue());
		uecOutStaffInfoVo.setIdNum(bean.getOutStaffIdNum().getValue());
		outStaffList();
	}
	
	/**
	 * 员工列表
	 * 
	 * @throws Exception
	 * @throws SuspendNotAllowedException
	 */
	public void outStaffList() throws SuspendNotAllowedException, Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("opType", "outStaffList");
		map.put("uecOutStaffInfoVo", uecOutStaffInfoVo);
		Window window = (Window) Executions.createComponents(
				UecOutStaffConstants.UEC_OUT_STAFF_LIST_ZUL, this, map);
		window.doModal();
		window.addEventListener("onOK", new EventListener() {
			@Override
			public void onEvent(Event event) throws Exception {
				if (event.getData() != null) {
					uecOutStaffInfo = (UecOutStaffInfo) event.getData();
					onQueryUecPsndocWadocList();
				}
			}
		});
	
	}

	/**
	 * 翻页
	 */
	public void onOutStaffAdjustPostApplyListboxPaging() {
		this.onQueryUecPsndocWadocList();
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecOutStaffInfo = null;
		uecOutStaffInfoVo = null;
		uecPsndocWadoc = null;
	}

	/**
	 * 查询
	 */
	public void onQueryUecPsndocWadocList() {
		if (null != uecOutStaffInfo) {
			PageInfo pageInfo = uecPsndocWadocManager.queryPsndocWadocPageInfoByUecOutStaffInfo(uecOutStaffInfo,
					this.bean.getOutStaffAdjustPostApplyListboxPaging().getActivePage() + 1,
					this.bean.getOutStaffAdjustPostApplyListboxPaging().getPageSize());
			ListModel list = new BindingListModelList(pageInfo.getDataList(), true);
			this.bean.getOutStaffAdjustPostApplyListbox().setModel(list);
			this.bean.getOutStaffAdjustPostApplyListboxPaging().setTotalSize(pageInfo.getTotalCount());
		}
	}

	/**
	 * 重置
	 */
	public void onReset() {
		clearVariable();
		bean.getOutStaffCode().setValue(null);
		bean.getOutStaffName().setValue(null);
		bean.getOutStaffIdNum().setValue(null);
		bean.getOutStaffPkHrorg().setValue(null);
		PubUtil.clearComponent(this);
		ListboxUtils.clearListbox(this.bean.getOutStaffAdjustPostApplyListbox());
		this.bean.getOutStaffAdjustPostApplyListboxPaging().setTotalSize(1);
	}

	/**
	 * 删除按钮
	 */
	public void onDelAdjustPost() {

	}

	/**
	 * 编辑按钮
	 */
	public void onEditUecPsndocWadoc() {

	}

	/**
	 * 编辑按钮
	 * 
	 * @throws Exception
	 * @throws SuspendNotAllowedException
	 */
	public void onAddAdjustPost() throws SuspendNotAllowedException, Exception {
		if(null != uecPsndocWadoc) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "add");
			map.put("uecOutStaffInfo", uecOutStaffInfo);
			map.put("uecPsndocWadoc", uecPsndocWadoc);
			Window window = (Window) Executions.createComponents(OUT_STAFF_ADJUST_POST_EDIT_APPLY_ZUL, this, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						//执行一次查询，刷新list列表数据
						onQueryUecPsndocWadocList();
					}
				}
			});
		}else {
			ZkUtil.showInformation("请选中需要编辑的记录", "温馨提示");
		}
	}

	/**
	 * 查看按钮
	 */
	public void onViewAdjustPost() {

	}

	/**
	 * 批量导入按钮
	 */
	public void onBatchImport() {

	}

	/**
	 * 模板下载按钮
	 */
	public void onTemplateDownload() {

	}

	/**
	 * 绑定下拉框
	 */
	public void bandbox() {
		
	}
}
